package com.example.tellwin.api;


import android.text.TextUtils;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.BaseObserver;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.home.bean.ActivityContentBean;
import com.example.tellwin.home.bean.ActivityDetailContentBean;
import com.example.tellwin.home.bean.ContentBean;
import com.example.tellwin.home.bean.ListContentBean;
import com.example.tellwin.home.bean.QuestionContentBean;
import com.example.tellwin.home.bean.UploadFileResultBean;
import com.example.tellwin.mine.bean.AttentionQuestionContentBean;
import com.example.tellwin.mine.bean.AttentionTeacherContentBean;
import com.example.tellwin.mine.bean.ExpenseRecordContentBean;
import com.example.tellwin.mine.bean.OrderContentBean;
import com.example.tellwin.mine.bean.ShareContentBean;
import com.example.tellwin.mine.bean.TeacherContentBean;
import com.example.tellwin.mine.bean.UserBean;
import com.example.tellwin.mine.bean.UserContentBean;
import com.example.tellwin.utils.NetUtil;
import com.example.tellwin.utils.ToastUtil;
import com.example.tellwin.view.LoadingDialog;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * 接口封装类
 * Created by wei.qt on 2017/4/7/007.
 */
public class TwjfApi {

    private static volatile TwjfApi instance = null;
    private TwjfApiService service;

    public static String serviceUrl = "https://tellwinedu.com/";

    public TwjfApi(OkHttpClient okHttpClient) {
//        String serviceUrl = VerifySDK.getInstance().getServerUrl();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(serviceUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 添加Rx适配器
                .addConverterFactory(GsonConverterFactory.create()) // 添加Gson转换器
                .client(okHttpClient)
                .build();
        service = retrofit.create(TwjfApiService.class);
    }

    private TwjfApi() {

    }

    public static TwjfApi getInstance(OkHttpClient okHttpClient) {
        // if already inited, no need to get lock everytime
        if (instance == null) {
            synchronized (TwjfApi.class) {
                if (instance == null) {
                    instance = new TwjfApi(okHttpClient);
                }
            }
        }
        return instance;
    }

    public void login(RequestBody requestBody, SimpleCallback simpleCallback) {
        setObservable(service.login(requestBody), simpleCallback);
    }

    public void getVerificationCode(RequestBody requestBody, SimpleCallback simpleCallback) {
        setObservable(service.getVerificationCode(requestBody), simpleCallback);
    }


    private void setObservable(Observable observable, SimpleCallback simpleCallback) {
        observable.subscribeOn(Schedulers.io())//放在异步中执行
                .observeOn(AndroidSchedulers.mainThread())//回到主线程;
                .subscribe(new BaseObserver<>(simpleCallback));
    }

    public void register(RequestBody requestBody, SimpleCallback<UserBean> userBeanSimpleCallback) {
        setObservable(service.register(requestBody), userBeanSimpleCallback);
    }

    public void homeBanner(SimpleCallback<ContentBean> listSimpleCallback) {
        setObservable(service.homeBanner(), listSimpleCallback);
    }

    public void homeColumn(SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.homeColumn(), simpleCallback);
    }

    public void indexQuestionList(String page, String pageSize, String columnId, SimpleCallback<QuestionContentBean> questionContentBeanSimpleCallback) {
        setObservable(service.indexQuestionList(page, pageSize, columnId), questionContentBeanSimpleCallback);
    }

    public void updateUserInfo(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.updateUserInfo(requestBody), objectSimpleCallback);
    }

    public void getTeacherTeam(SimpleCallback<ContentBean> objectSimpleCallback) {
        setObservable(service.getTeacherTeam(), objectSimpleCallback);
    }

    public void getActivityList(String page, String pageSize, SimpleCallback<ActivityContentBean> simpleCallback) {
        setObservable(service.getActivityList(page, pageSize), simpleCallback);
    }

    public void follow(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.follow(requestBody), objectSimpleCallback);
    }

    public void getActivityDetail(String actId, SimpleCallback<ActivityDetailContentBean> simpleCallback) {
        setObservable(service.getActivityDetail(actId), simpleCallback);
    }

    public void uploadFile(RequestBody requestBody, SimpleCallback<String> stringSimpleCallback) {
        LoadingDialog loadingDialog = new LoadingDialog(MainApplication.getCurActivity());
        loadingDialog.show();
        service.uploadFile(requestBody).subscribeOn(Schedulers.io())//放在异步中执行
                .observeOn(AndroidSchedulers.mainThread())//回到主线程;
                .subscribe(new Observer<UploadFileResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        stringSimpleCallback.onLoading();
                    }

                    @Override
                    public void onNext(UploadFileResultBean uploadFileResultBean) {
                        if (uploadFileResultBean.getData() != null && !TextUtils.isEmpty(uploadFileResultBean.getData().getSrc())) {
                            stringSimpleCallback.onSuccess(uploadFileResultBean.getData().getSrc());
                        } else {
                            ToastUtil.show(MainApplication.getContext(), uploadFileResultBean.getMsg());
                            stringSimpleCallback.onFail(uploadFileResultBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (NetUtil.getNetWorkStatus(MainApplication.getContext()) == -1) {
                            ToastUtil.show(MainApplication.getContext(), R.string.currently_no_network);
                        } else if (e instanceof SocketTimeoutException) {
                            ToastUtil.show(MainApplication.getContext(), R.string.timeout);
                        } else if (e instanceof ConnectException) {
                            ToastUtil.show(MainApplication.getContext(), R.string.network_interruption);
                        } else {
                            ToastUtil.show(MainApplication.getContext(), e.getMessage());
                        }
                        stringSimpleCallback.onFail(e.getMessage());
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onComplete() {
                        stringSimpleCallback.onComplete();
                        loadingDialog.dismiss();
                    }
                });

    }

    public void uploadFileNotDialog(RequestBody requestBody, SimpleCallback<String> stringSimpleCallback) {
        service.uploadFile(requestBody).subscribeOn(Schedulers.io())//放在异步中执行
                .observeOn(AndroidSchedulers.mainThread())//回到主线程;
                .subscribe(new Observer<UploadFileResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        stringSimpleCallback.onLoading();
                    }

                    @Override
                    public void onNext(UploadFileResultBean uploadFileResultBean) {
                        if (uploadFileResultBean.getData() != null && !TextUtils.isEmpty(uploadFileResultBean.getData().getSrc())) {
                            stringSimpleCallback.onSuccess(uploadFileResultBean.getData().getSrc());
                        } else {
                            ToastUtil.show(MainApplication.getContext(), uploadFileResultBean.getMsg());
                            stringSimpleCallback.onFail(uploadFileResultBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (NetUtil.getNetWorkStatus(MainApplication.getContext()) == -1) {
                            ToastUtil.show(MainApplication.getContext(), R.string.currently_no_network);
                        } else if (e instanceof SocketTimeoutException) {
                            ToastUtil.show(MainApplication.getContext(), R.string.timeout);
                        } else if (e instanceof ConnectException) {
                            ToastUtil.show(MainApplication.getContext(), R.string.network_interruption);
                        } else {
                            ToastUtil.show(MainApplication.getContext(), e.getMessage());
                        }
                        stringSimpleCallback.onFail(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        stringSimpleCallback.onComplete();
                    }
                });

    }

    public void participateActivity(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.participateActivity(requestBody), objectSimpleCallback);
    }

    public void workVote(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.workVote(requestBody), objectSimpleCallback);
    }

    public void historySearch(SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.historySearch(), simpleCallback);
    }

    public void hotSearch(SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.hotSearch(), simpleCallback);
    }

    public void search(String searchName, String pageNum, String pageSize, SimpleCallback<QuestionContentBean> simpleCallback) {
        setObservable(service.search(searchName, pageNum, pageSize), simpleCallback);
    }

    public void getQuestionDetail(String questionId, SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.getQuestionDetail(questionId), simpleCallback);
    }

    public void getTeacherDetail(String teacherId, SimpleCallback<TeacherContentBean> simpleCallback) {
        setObservable(service.getTeacherDetail(teacherId), simpleCallback);
    }

    public void getUserInfo(SimpleCallback<UserContentBean> objectSimpleCallback) {
        setObservable(service.getUserInfo(), objectSimpleCallback);
    }

    public void getGradeList(SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.getGradeList(), simpleCallback);
    }

    public void getSubjectList(SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.getSubjectList(), simpleCallback);
    }

    public void getQualificationsList(SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.getQualificationsList(), simpleCallback);
    }

    public void getMandarinList(SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.getMandarinList(), simpleCallback);
    }

    public void applyAnswer(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.applyAnswer(requestBody), objectSimpleCallback);
    }

    public void applyDetail(SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.applyDetail(), simpleCallback);
    }

    public void myAccount(String page, String pageSize, SimpleCallback<ExpenseRecordContentBean> expenseRecordContentBeanSimpleCallback) {
        setObservable(service.myAccount(page, pageSize), expenseRecordContentBeanSimpleCallback);
    }

    public void getBillingInstructions(SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.getBillingInstructions(), simpleCallback);
    }

    public void getOrderList(String status, String page, String pageSize, SimpleCallback<OrderContentBean> simpleCallback) {
        setObservable(service.getOrderList(status, page, pageSize), simpleCallback);
    }

    public void getAttentionQuestionList(String relationType, String page, String pageSize, SimpleCallback<AttentionQuestionContentBean> attentionContentBeanSimpleCallback) {
        setObservable(service.getAttentionQuestionList(relationType, page, pageSize), attentionContentBeanSimpleCallback);
    }

    public void getAttentionTeacherList(String relationType, String page, String pageSize, SimpleCallback<AttentionTeacherContentBean> attentionContentBeanSimpleCallback) {
        setObservable(service.getAttentionTeacherList(relationType, page, pageSize), attentionContentBeanSimpleCallback);
    }

    public void uploadQuestion(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.uploadQuestion(requestBody), objectSimpleCallback);
    }

    public void getOrderDetail(String orderId, SimpleCallback<ContentBean> contentBeanSimpleCallback) {
        setObservable(service.getOrderDetail(orderId), contentBeanSimpleCallback);
    }

    public void cancelOrder(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.cancelOrder(requestBody), objectSimpleCallback);
    }

    public void getTopStatistics(SimpleCallback<ContentBean> contentBeanSimpleCallback) {
        setObservable(service.getTopStatistics(), contentBeanSimpleCallback);
    }

    public void getAnswerQuestionList(String page, String pageSize, SimpleCallback<ListContentBean> contentBeanSimpleCallback) {
        setObservable(service.getAnswerQuestionList(page, pageSize), contentBeanSimpleCallback);
    }

    public void meetOrder(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.meetOrder(requestBody), objectSimpleCallback);
    }

    public void selectAnswerType(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.selectAnswerType(requestBody), objectSimpleCallback);
    }

    public void userHeadPic(String userId, SimpleCallback<ContentBean> contentBeanSimpleCallback) {
        setObservable(service.userHeadPic(userId), contentBeanSimpleCallback);
    }

    public void answerSuccess(RequestBody requestBody, SimpleCallback<Object> stringSimpleCallback) {
        setObservable(service.answerSuccess(requestBody), stringSimpleCallback);
    }

    public void switchIdentity(RequestBody requestBody, SimpleCallback<UserBean> userBeanSimpleCallback) {
        setObservable(service.switchIdentity(requestBody), userBeanSimpleCallback);
    }

    public void sendOrder(RequestBody requestBody, SimpleCallback<Object> simpleCallback) {
        setObservable(service.sendOrder(requestBody), simpleCallback);
    }

    public void orderPay(RequestBody requestBody, SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.orderPay(requestBody), simpleCallback);
    }

    public void recharge(RequestBody requestBody, SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.recharge(requestBody), simpleCallback);
    }

    public void payQuery(String payId, String type, SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.payQuery(payId, type), simpleCallback);
    }

    public void orderEvaluate(RequestBody requestBody, SimpleCallback<ContentBean> simpleCallback) {
        setObservable(service.orderEvaluate(requestBody), simpleCallback);
    }

    public void getRegulation(SimpleCallback<ContentBean> contentBeanSimpleCallback) {
        setObservable(service.getRegulation(), contentBeanSimpleCallback);
    }

    public void creditsExchange(RequestBody requestBody, SimpleCallback<ContentBean> contentBeanSimpleCallback) {
        setObservable(service.creditsExchange(requestBody), contentBeanSimpleCallback);
    }

    public void newOrder(RequestBody requestBody, SimpleCallback<ContentBean> objectSimpleCallback) {
        setObservable(service.newOrder(requestBody), objectSimpleCallback);

    }

    public void cancelTakeOrder(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.cancelTakeOrder(requestBody), objectSimpleCallback);
    }

    public void feedback(RequestBody requestBody, SimpleCallback<Object> objectSimpleCallback) {
        setObservable(service.feedback(requestBody), objectSimpleCallback);
    }

    public void withDrawal(RequestBody requestBody, SimpleCallback<ContentBean> contentBeanSimpleCallback) {
        setObservable(service.withDrawal(requestBody), contentBeanSimpleCallback);
    }

    public void share(RequestBody requestBody, SimpleCallback<Object> simpleCallback) {
        setObservable(service.share(requestBody), simpleCallback);
    }

    public void shareList(String page, String pageSize, SimpleCallback<ShareContentBean> shareContentBeanSimpleCallback) {
        setObservable(service.shareList(page, pageSize), shareContentBeanSimpleCallback);
    }

    public void getAliAuthToken(SimpleCallback<ContentBean> contentBeanSimpleCallback) {
        setObservable(service.getAliAuthToken(), contentBeanSimpleCallback);
    }

    public void uploadAliToken(RequestBody requestBody, SimpleCallback<ContentBean> contentBeanSimpleCallback) {
        setObservable(service.uploadAliToken(requestBody), contentBeanSimpleCallback);
    }

    public void getWithDrawalHint(SimpleCallback<ContentBean> contentBeanSimpleCallback) {
        setObservable(service.getWithDrawalHint(), contentBeanSimpleCallback);
    }
}
